package cn.pfz.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.pfz.Service.IAppointmentMapperService;
import cn.pfz.Service.IBusinessService;
import cn.pfz.Service.ISystemDictionaryItemSerives;
import cn.pfz.domain.*;

import cn.pfz.query.appointmentQuery;
import cn.pfz.statusnum.AppointmentstatusEnum;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/appointment")
public class AppointmentController {

    @Autowired
    private IAppointmentMapperService appointmentMapperService;
    @Autowired
    private ISystemDictionaryItemSerives systemDictionaryItemSerives;
    @Autowired
    private IBusinessService businessService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") appointmentQuery qo) {
        List<SystemDictionaryItem> systemDictionaryItemList = systemDictionaryItemSerives.selectByTpyeid(1);
        AppointmentstatusEnum[] values = AppointmentstatusEnum.values();

        PageInfo<Appointment> list = appointmentMapperService.list(qo);
        List<Business> businesses = businessService.selectAll();
        model.addAttribute("businesses",businesses);
        model.addAttribute("systemDictionaryItemList",systemDictionaryItemList);
        model.addAttribute("statues",values);
        model.addAttribute("result",list);
        return "appointment/list";
    }


    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Appointment appointment, HttpServletRequest req){
        appointment.setCreate_time(new Date());
        appointment.setStatus(0);
        String format = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        String s = RandomUtil.randomNumbers(5);
        appointment.setAno(format+s);
        if(appointment.getId()!=null){
            //修改
            appointmentMapperService.updateByPrimaryKey(appointment);
        }else{
            appointmentMapperService.insert(appointment);
        }
        return "redirect:/appointment/list";
    }

    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(Long id) {
        if (appointmentMapperService.selectByPrimaryKey(id).getStatus().intValue()!=4){
                    return JsonResult.erro("只有废档才能删除");
        }
        
    

     
        
        appointmentMapperService.deleteByPrimaryKey(id);
        return JsonResult.success(null);
    }

    @RequestMapping("/saveOrUpdateasync")
    @ResponseBody
    public JsonResult saveOrUpdate_async(Appointment appointment){
           appointmentMapperService.updateStatusById(appointment);
        return JsonResult.success(null);
    }
    

}
